package cn.edu.lingnan.servlet;

import cn.edu.lingnan.service.IParticipationService;
import cn.edu.lingnan.service.IStudentService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
@WebServlet("/registerPart")
public class RegisterPart extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        int sid = (int) session.getAttribute("sid");
        int cid = Integer.parseInt(req.getParameter("cid"));
        String cname = req.getParameter("cname");

        IParticipationService service = new IParticipationService();
        IStudentService service1 = new IStudentService();

        boolean flag = service.addParticipation(sid,cid,cname);
        if(flag){
            service1.updateStuFlag(sid);//将flag标志位设为1，表示该学生已经报名竞赛
            resp.sendRedirect("/user/success.jsp");//报名成功
        }else {
            resp.sendRedirect("/user/success2.jsp");//报名失败
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
